package com.mall.controller;

import com.mall.domain.User;
import com.mall.service.impl.UserServiceImpl;
import com.mall.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Bei-Zhen
 * @date: 2020-12-25 11:51
 * @description: 用户web层
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    /**
     * 测试
     * @param loginUser
     * @return
     */
    @RequestMapping("/login")
    public  @ResponseBody
    Map<String,Object> login(@RequestBody User loginUser) {
        Map<String,Object> loginMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        String token = TokenUtil.sign(loginUser.getUsername());
        dataMap.put("token",token);
        loginMap.put("data",dataMap);
        Map<String,Object> metaMap = new HashMap<>();
        User user = userService.login(loginUser);
        if(user != null){
            metaMap.put("msg","登录成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","登录失败");
            metaMap.put("status",0);
        }
        loginMap.put("meta",metaMap);
        return  loginMap;
    }

}
